
#srcDir = "/home/michael/debs/data/"
#outDir = "/home/michael/debs/out/"
#swcs_outDir = "/home/michael/debs/swc_out/"


#srcDir = "/home/michael/debs/data/"
#outDir = "/home/michael/debs/out/"
#swcs_outDir = "/home/michael/debs/swc_out/"


#srcDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/xyz raw/"
#outDir = "C:/testout/out/"
#swcs_outDir = "C:/testout/swc_out/"


srcDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/xyz raw/"
outDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/intermed/"
swcs_outDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/swc final/"

import os


from nrnConcatenate import nrnConcatenateXYZS
from nrnInvertX import nrnInvertX
from nrnStraighten import nrnStraighten
from nrnTranslate import nrnTranslate
from nrnXYZToSWC import nrnXYZToSWC
from nrnScale import nrnScale

import shutil

def nrnDoAll( nrnType, nrnNumber, nrnVersion="",nrnDate=""):
    versionSuffix = "" if not nrnVersion else " "+nrnVersion
    dateSuffix = "" if not nrnDate else " " + nrnDate 
    srcFileNRN = srcDir+"%s %s%s/%s %s nrn%s%s.txt"%(nrnType,nrnNumber, versionSuffix,nrnType,nrnNumber, dateSuffix, versionSuffix)
    srcFileNS =  srcDir+"%s %s%s/%s %s ns%s%s.txt"%(nrnType,nrnNumber, versionSuffix,nrnType,nrnNumber, dateSuffix,  versionSuffix)
    
    #testing
    #print "nrnType = " + nrnType
    #print "nrnNumber = " + nrnNumber
    #print "versionSuffix = " + versionSuffix
    #print "dateSuffix = " + dateSuffix
    
    transFileNRN = outDir+"%s %s%s/%s %s nrn%s%s.transl.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    transFileNS = outDir+"%s %s%s/%s %s ns%s%s.transl.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    invTransFileNRN = outDir+"%s %s%s/%s %s nrn%s%s.transl.invX.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    invTransFileNS = outDir+"%s %s%s/%s %s ns%s%s.transl.invX.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)

    scaledInvTransFileNRN = outDir+"%s %s%s/%s %s nrn%s%s.transl.invX.scaled.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    scaledInvTransFileNS = outDir+"%s %s%s/%s %s ns%s%s.transl.invX.scaled.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)

    
    invTransFileConCat = outDir+"%s %s%s/%s %s nrn + ns%s%s.transl.invX.scaled.txt"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    invTransFileConCatSWC = outDir+"%s %s%s/%s %s nrn + ns%s%s.transl.invX.scaled.swc"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    matlabscriptname = outDir+"%s %s%s/mscript.m"%(nrnType,nrnNumber,versionSuffix) #,nrnType,nrnNumber,versionSuffix)
    invTransFileConCatSWCStraight = outDir+"%s %s%s/%s %s nrn + ns%s%s.transl.invX.scaled.straightened.swc"%(nrnType,nrnNumber,versionSuffix,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    
    
    final = swcs_outDir+"%s %s nrn + ns%s%s.transl.invX.scaled.straightened.swc"%(nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    
    
    print "Source Files"
    print "\t", srcFileNRN
    print "\t", srcFileNS 
    
    print "Producing"
    print "\t",transFileNRN
    print "\t",transFileNS
    
    print "\t",invTransFileNRN
    print "\t",invTransFileNS
    
    print "\t",scaledInvTransFileNRN
    print "\t",scaledInvTransFileNS
    
    print "\t",invTransFileConCat
    print "\t",invTransFileConCatSWC
    print "\t",invTransFileConCatSWCStraight
    
    
    print "Executing:"
    
    
    nrnTranslate(  
        nsFilenameSrc=srcFileNS, 
        nrnFilenameSrc=srcFileNRN, 
        nsFilenameSink=transFileNS, 
        nrnFilenameSink=transFileNRN)
    
    nrnInvertX( srcFilename=transFileNS, sinkFilename=invTransFileNS)
    nrnInvertX( srcFilename=transFileNRN, sinkFilename=invTransFileNRN) 
    
    nrnScale(srcFilename=invTransFileNS, sinkFilename=scaledInvTransFileNS, scaleFactor=1.28)
    nrnScale(srcFilename=invTransFileNRN, sinkFilename=scaledInvTransFileNRN, scaleFactor=1.28)
        
    nrnConcatenateXYZS(  srcFilename1=scaledInvTransFileNRN, srcFilename2=scaledInvTransFileNS, sinkFilename=invTransFileConCat)
    
    nrnXYZToSWC(srcFilename=invTransFileConCat, sinkFilename=invTransFileConCatSWC)
    
    nrnStraighten( srcFilename = invTransFileConCatSWC, sinkFilename=invTransFileConCatSWCStraight,matlabscriptname=matlabscriptname)
    if not os.path.exists(swcs_outDir): os.makedirs(swcs_outDir)
	
    shutil.copyfile(invTransFileConCatSWCStraight,final)
    
    
